﻿Imports System.Xml.Linq
Imports System.Text

Namespace Entities
    Public Class CommandSet
        Public Sub New()
            _Commands = New Dictionary(Of String, Command)
            _PhraseLists = New Dictionary(Of String, PhraseList)
        End Sub

        Public Property Culture As String
        Public Property Name As String
        Public Property CommandPrefix As String
        Public Property Example As String

        Private ReadOnly _Commands As IDictionary(Of String, Command)
        Public ReadOnly Property Commands As IEnumerable(Of Command)
            Get
                Return _Commands.Values
            End Get
        End Property

        Private ReadOnly _PhraseLists As IDictionary(Of String, PhraseList)
        Public ReadOnly Property PhraseLists As IEnumerable(Of PhraseList)
            Get
                Return _PhraseLists.Values
            End Get
        End Property

        Public Function AddCommand(command As Command) As Boolean
            If command Is Nothing Then Throw New ArgumentNullException()
            If Not command.Validate Then Throw New ArgumentException("Command invalid")
            If _Commands.ContainsKey(command.Name) Then
                Throw New ArgumentException("Command already exists")
            Else
                Me._Commands(command.Name) = command
            End If
            Return True
        End Function

        Public Function RemoveCommand(commandName As String) As Boolean
            If String.IsNullOrWhiteSpace(commandName) Then Throw New ArgumentNullException()
            If _Commands.ContainsKey(commandName) Then
                _Commands.Remove(commandName)
                Return True
            Else
                Return False
            End If
        End Function

        Public Function AddPhraseList(phraseList As PhraseList) As Boolean
            If phraseList Is Nothing Then Throw New ArgumentNullException()
            If Not phraseList.Validate Then Throw New ArgumentException("phraseList invalid")
            If _PhraseLists.ContainsKey(phraseList.Label) Then
                Throw New ArgumentException("phraseList already exists")
            Else
                Me._PhraseLists(phraseList.Label) = phraseList
            End If
            Return True
        End Function

        Public Function RemovePhraseList(phraseListLabel As String) As Boolean
            If String.IsNullOrWhiteSpace(phraseListLabel) Then Throw New ArgumentNullException()
            If _PhraseLists.ContainsKey(phraseListLabel) Then
                _PhraseLists.Remove(phraseListLabel)
                Return True
            Else
                Return False
            End If
        End Function

        Public Function GetXml() As String
            If Not Validate() Then Return Nothing
            Dim strBuild As New StringBuilder
            strBuild.AppendFormat("<CommandSet xml:lang=""{0}"" Name=""{1}"">", Culture, Name)
            strBuild.AppendLine()
            strBuild.AppendFormat("<CommandPrefix>{0}</CommandPrefix>", CommandPrefix)
            strBuild.AppendLine()
            strBuild.AppendFormat("<Example>{0}</Example>", Example)
            strBuild.AppendLine()
            For Each k In _Commands.Keys
                _Commands(k).Name = k
                strBuild.AppendLine(_Commands(k).GetXml())
            Next
            For Each k In _PhraseLists.Keys
                _PhraseLists(k).Label = k
                strBuild.AppendLine(_PhraseLists(k).GetXml())
            Next
            strBuild.AppendLine("</CommandSet>")

            Return strBuild.ToString
        End Function

        Public Function Validate() As Boolean
            If String.IsNullOrWhiteSpace(Name) Then Return False
            If String.IsNullOrWhiteSpace(Culture) Then Return False
            If String.IsNullOrWhiteSpace(CommandPrefix) Then Return False
            Return True
        End Function
    End Class
End Namespace